/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buspirategui;

import java.util.Calendar;
import javax.swing.DefaultComboBoxModel;
import jssc.SerialPortList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author PiotrGl
 */
public class BusPirateGUI extends javax.swing.JFrame {

    static private DefaultTableModel SPImodel = new DefaultTableModel(); 
        
    public BusPirateDevice pirate = new BusPirateDevice();

    /**
     * Creates new form gui
     */
    public BusPirateGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jIOPanel = new javax.swing.JPanel();
        jSPIPanel = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jSPITable = new javax.swing.JTable();
        jI2CPanel = new javax.swing.JPanel();
        jUartPanel = new javax.swing.JPanel();
        j1WirePanel = new javax.swing.JPanel();
        jRawPanel = new javax.swing.JPanel();
        jSettingsPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSettingsButtionConnect = new javax.swing.JButton();
        jSettingsPortsBox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jSetModeBox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLogText = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        javax.swing.GroupLayout jIOPanelLayout = new javax.swing.GroupLayout(jIOPanel);
        jIOPanel.setLayout(jIOPanelLayout);
        jIOPanelLayout.setHorizontalGroup(
            jIOPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        jIOPanelLayout.setVerticalGroup(
            jIOPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("IO", jIOPanel);

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jSPITable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Czas", "adress", "dane", "Title 4"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jSPITable);

        javax.swing.GroupLayout jSPIPanelLayout = new javax.swing.GroupLayout(jSPIPanel);
        jSPIPanel.setLayout(jSPIPanelLayout);
        jSPIPanelLayout.setHorizontalGroup(
            jSPIPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jSPIPanelLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jSPIPanelLayout.setVerticalGroup(
            jSPIPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jSPIPanelLayout.createSequentialGroup()
                .addGroup(jSPIPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jSPIPanelLayout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jButton2))
                    .addGroup(jSPIPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("SPI", jSPIPanel);

        javax.swing.GroupLayout jI2CPanelLayout = new javax.swing.GroupLayout(jI2CPanel);
        jI2CPanel.setLayout(jI2CPanelLayout);
        jI2CPanelLayout.setHorizontalGroup(
            jI2CPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        jI2CPanelLayout.setVerticalGroup(
            jI2CPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("I2C", jI2CPanel);

        javax.swing.GroupLayout jUartPanelLayout = new javax.swing.GroupLayout(jUartPanel);
        jUartPanel.setLayout(jUartPanelLayout);
        jUartPanelLayout.setHorizontalGroup(
            jUartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        jUartPanelLayout.setVerticalGroup(
            jUartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("UART", jUartPanel);

        javax.swing.GroupLayout j1WirePanelLayout = new javax.swing.GroupLayout(j1WirePanel);
        j1WirePanel.setLayout(j1WirePanelLayout);
        j1WirePanelLayout.setHorizontalGroup(
            j1WirePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        j1WirePanelLayout.setVerticalGroup(
            j1WirePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("1-WIRE", j1WirePanel);

        javax.swing.GroupLayout jRawPanelLayout = new javax.swing.GroupLayout(jRawPanel);
        jRawPanel.setLayout(jRawPanelLayout);
        jRawPanelLayout.setHorizontalGroup(
            jRawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 595, Short.MAX_VALUE)
        );
        jRawPanelLayout.setVerticalGroup(
            jRawPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 450, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("RAW", jRawPanel);

        jLabel1.setText("Port:");

        jSettingsButtionConnect.setText("CONNECT");
        jSettingsButtionConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSettingsButtionConnectActionPerformed(evt);
            }
        });

        jSettingsPortsBox.setEditable(true);
        jSettingsPortsBox.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jSettingsPortsBoxPopupMenuWillBecomeVisible(evt);
            }
        });

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setText("jTextField1");

        jSetModeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NOMODE", "BBIO1", "SPI", "I2C", "ART1", "1-Wire", "Raw" }));

        javax.swing.GroupLayout jSettingsPanelLayout = new javax.swing.GroupLayout(jSettingsPanel);
        jSettingsPanel.setLayout(jSettingsPanelLayout);
        jSettingsPanelLayout.setHorizontalGroup(
            jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jSettingsPanelLayout.createSequentialGroup()
                        .addComponent(jSetModeBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(jSettingsPanelLayout.createSequentialGroup()
                        .addGroup(jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jSettingsPortsBox, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSettingsButtionConnect)))
                .addContainerGap(358, Short.MAX_VALUE))
        );
        jSettingsPanelLayout.setVerticalGroup(
            jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jSettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jSettingsButtionConnect)
                        .addComponent(jSettingsPortsBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jSettingsPanelLayout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel1)))
                .addGap(18, 18, 18)
                .addGroup(jSettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSetModeBox))
                .addGap(32, 32, 32)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(320, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("SETTINGS", jSettingsPanel);

        jLogText.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLogText.setText("Hello World");
        jLogText.setEnabled(false);
        jScrollPane1.setViewportView(jLogText);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jSettingsButtionConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSettingsButtionConnectActionPerformed
        if (pirate.getPort() != null) {
            if (pirate.Disconnect()== 0) {
                jLogText.setText("Disconected");
                jSettingsButtionConnect.setText("CONNECT");
            } else {
                jLogText.setText("Error");
            }
        } else {
            if (pirate.Connect(jSettingsPortsBox.getSelectedItem().toString()) == 0) {
                jLogText.setText("Connected");
                jSettingsButtionConnect.setText("DISCONNECT");
            } else {
                jLogText.setText("Error");
            }
        }
        

    }//GEN-LAST:event_jSettingsButtionConnectActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (pirate.getPort() != null) {
            pirate.Disconnect();
        }
    }//GEN-LAST:event_formWindowClosing

    private void jSettingsPortsBoxPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jSettingsPortsBoxPopupMenuWillBecomeVisible
        String[] portNames = SerialPortList.getPortNames();
        //for(int i = 0; i < portNames.length; i++){
        jSettingsPortsBox.setModel(new DefaultComboBoxModel(portNames));
        //    System.out.println(portNames[i]);
        //}
    }//GEN-LAST:event_jSettingsPortsBoxPopupMenuWillBecomeVisible

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (pirate.getPort() != null) {
            pirate.setMode(jSetModeBox.getSelectedIndex());
            
            //jSetModeBox.setSelectedItem(pirate.getMode());
            
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        Calendar cal = Calendar.getInstance();
        System.out.println(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND)+","+cal.get(Calendar.MILLISECOND));
        
        System.out.println(System.currentTimeMillis());
         SPImodel.addRow(new Object[]{"v1", "v2", "v3", "V4"});
        
        jSPITable.setModel(SPImodel);

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BusPirateGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BusPirateGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BusPirateGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BusPirateGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //initialization table
        SPImodel.addColumn("Czas"); 
        SPImodel.addColumn("Adres"); 
        SPImodel.addColumn("Dane"); 
        SPImodel.addColumn("Opis"); 
        //jTable1.setModel(SPImodel);
        
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BusPirateGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel j1WirePanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jI2CPanel;
    private javax.swing.JPanel jIOPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextPane jLogText;
    private javax.swing.JPanel jRawPanel;
    private javax.swing.JPanel jSPIPanel;
    private javax.swing.JTable jSPITable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JComboBox jSetModeBox;
    private javax.swing.JButton jSettingsButtionConnect;
    private javax.swing.JPanel jSettingsPanel;
    private javax.swing.JComboBox jSettingsPortsBox;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel jUartPanel;
    // End of variables declaration//GEN-END:variables
}
